package com.trendplus.unitconverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class AreaCalculate extends Activity {
	Spinner areaType=null; 
	TextView mTextView;
	private EditText inputValue;
	private Button button_calc;
	private Button button_cleanResult;
	/** Called when the activity is first created. */
	//catty:台斤, Taiwan Tael:台兩, 
    static final String[] areaTypeList=new String[] {
        "平方公尺", "公畝", "公頃", "坪","甲"
    };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.calc_area);
	    findViews();
	    touchSpinnerS();
	    setListeners();
	}
	public void findViews(){
		inputValue = (EditText) findViewById(R.id.inputValue);
		button_calc = (Button) findViewById(R.id.submit);
		button_cleanResult = (Button) findViewById(R.id.cleanData);
	}
	//Listen for button clicks
    private void setListeners() {
        button_calc.setOnClickListener(calcArea);
        button_cleanResult.setOnClickListener(cleanEditText);
    }
    
    public void touchSpinnerS(){
		Spinner SpinnerS = (Spinner)findViewById(R.id.mySpinner);

        //設定功能表項目陣列，使用createFromResource()
        //ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.items, android.R.layout.simple_spinner_item);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, areaTypeList);

        //設定選單
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mTextView = (TextView) findViewById(R.id.textViewArea);
        //設定adapter
        SpinnerS.setAdapter(adapter);
        
        SpinnerS.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                mTextView.setText(areaTypeList[arg2]);
                arg0.setVisibility(View.VISIBLE);
            }
 
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
 
        });
	}
    private OnClickListener calcArea = new OnClickListener(){
		public void onClick(View v) {
			TextView areaType = (TextView)findViewById(R.id.textViewArea);
			EditText inputValue = (EditText)findViewById(R.id.inputValue);
			String areaTypes = areaType.getText().toString();
			long inputValues = Long.parseLong((inputValue.getText().toString()));
			System.out.println("areaTypes="+areaTypes+", values="+inputValues);
			
			converterOfArea(areaTypes,inputValues);
		}
    };
    
    public void converterOfArea(String areaTypes, double value){
		String m2ResultStr = "";
		String muResultStr = "";
		String hectaresResultStr = "";
		String pingResultStr = "";
		String chiaResultStr = "";
	
		if(areaTypes.equals("平方公尺")){
			m2ResultStr = Double.toString(value);
			muResultStr = Double.toString(value*0.01);
			hectaresResultStr = Double.toString(value*0.0001);
			pingResultStr = Double.toString(value*0.3025);
			chiaResultStr = Double.toString(value*0.000103);
			resultSetting(m2ResultStr, muResultStr, hectaresResultStr, pingResultStr, chiaResultStr);
			
			
		}else if(areaTypes.equals("公畝")){
			m2ResultStr = Double.toString(value*100);
			muResultStr = Double.toString(value);
			hectaresResultStr = Double.toString(value*0.01);
			pingResultStr = Double.toString(value*30.25);
			chiaResultStr = Double.toString(value*0.01031);
			resultSetting(m2ResultStr, muResultStr, hectaresResultStr, pingResultStr, chiaResultStr);
			
		}else if(areaTypes.equals("公頃")){
			m2ResultStr = Double.toString(value*10000);
			muResultStr = Double.toString(value*100);
			hectaresResultStr = Double.toString(value);
			pingResultStr = Double.toString(value*3025);
			chiaResultStr = Double.toString(value*1.03102);
			resultSetting(m2ResultStr, muResultStr, hectaresResultStr, pingResultStr, chiaResultStr);
			
		}else if(areaTypes.equals("坪")){
			m2ResultStr = Double.toString(value*3.30579);
			muResultStr = Double.toString(value*0.03306);
			hectaresResultStr = Double.toString(value*0.00033);
			pingResultStr = Double.toString(value);
			chiaResultStr = Double.toString(value*0.00034);
			resultSetting(m2ResultStr, muResultStr, hectaresResultStr, pingResultStr, chiaResultStr);
			
		}else if(areaTypes.equals("甲")){
			m2ResultStr = Double.toString(value*9699.17);
			muResultStr = Double.toString(value*96.9917);
			hectaresResultStr = Double.toString(value*0.96992);
			pingResultStr = Double.toString(value*2934);
			chiaResultStr = Double.toString(value);
			resultSetting(m2ResultStr, muResultStr, hectaresResultStr, pingResultStr, chiaResultStr);
			
		}
	}

	private OnClickListener cleanEditText = new OnClickListener(){
	
		public void onClick(View v) {
			resultSetting("__", "__", "__", "__", "__");
		}
	};
	
	//結果填入欄位內
	public void resultSetting(String m2ResultStr, String  muResultStr,
			String  hectaresResultStr, String  pingResultStr, String  chiaResultStr){
		
		TextView m2Result = (TextView)findViewById(R.id.m2Result);
		TextView muResult = (TextView)findViewById(R.id.muResult);
		TextView hectaresResult = (TextView)findViewById(R.id.hectaresResult);
		TextView pingResult = (TextView)findViewById(R.id.pingResult);
		TextView chiaResult = (TextView)findViewById(R.id.chiaResult);
		
		m2Result.setText("="+ m2ResultStr +"平方公尺");
		muResult.setText("="+ muResultStr +"公畝");
		hectaresResult.setText("="+ hectaresResultStr +"公頃");
		pingResult.setText("="+ pingResultStr +"坪");
		chiaResult.setText("="+ chiaResultStr +"甲");
	}
}
